package com.yvon.maple.service.system.controller;

import cn.hutool.core.collection.CollUtil;
import com.yvon.boot.common.result.ResponseData;
import com.yvon.boot.common.validation.group.CreateGroup;
import com.yvon.boot.common.validation.group.UpdateGroup;
import com.yvon.boot.mybatis.page.ListData;
import com.yvon.boot.mybatis.search.PageQuery;
import com.yvon.maple.cloud.web.data.AbstractController;
import com.yvon.maple.service.system.dto.RoleDTO;
import com.yvon.maple.service.system.dto.RolePermissionDTO;
import com.yvon.maple.service.system.entity.Role;
import com.yvon.maple.service.system.vo.RoleVO;
import com.yvon.maple.service.system.service.RolePermissionService;
import com.yvon.maple.service.system.service.RoleService;
import com.yvon.maple.utils.DataConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : Yvon
 * @version : 1.0
 * @since : 2021/3/15
 */
@RestController
@RequestMapping(value = "/roles")
@Api(value = "角色管理", tags = "系统服务 - 角色管理API")
public class RoleController extends AbstractController<Role, RoleDTO, RoleVO, RoleService> {

    @Autowired
    private RolePermissionService rolePermissionService;

    @ApiOperation(value = "新增")
    @PostMapping
    public ResponseData<RoleDTO> add(@Validated(CreateGroup.class) @RequestBody RoleVO roleVO) {
        Role role = voToEntity(roleVO);
        getService().save(role, roleVO.getPermissionIds());
        return success(entityToDto(role));
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public ResponseData<RoleDTO> update(@Validated(UpdateGroup.class) @RequestBody RoleVO roleVO) {
        Role role = voToEntity(roleVO);
        getService().update(role, roleVO.getPermissionIds());
        return success(entityToDto(role));
    }

    @ApiOperation(value = "查询角色详细信息")
    @GetMapping("/code/{code}")
    public ResponseData<RoleDTO> getDetailByCode(@PathVariable("code") String code) {
        Role role = getService().getByCode(code);
        return success(entityToDTO(role));
    }

    @GetMapping({"/page"})
    @ApiOperation(value = "分页查询", notes = "分页查询API")
    public ResponseData<ListData<RoleDTO>> findPage(PageQuery pageQuery) {
        ListData listData = getService().page(pageQuery);
        listData.setList(this.entityToDTO(listData.getList()));
        return success(listData);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表API")
    public ResponseData<List<RoleDTO>> findList() {
        List<Role> list = getService().list();
        return success(DataConverter.convert(list, RoleDTO.class));
    }

    protected RoleDTO entityToDTO(Role entity) {
        RoleDTO roleDTO = super.entityToDto(entity);
        if (Objects.nonNull(roleDTO)) {
            roleDTO.setPermissions(rolePermissionService.getByRoleId(roleDTO.getId()));
        }
        return roleDTO;
    }


    protected List<RoleDTO> entityToDTO(List<Role> entityList) {
        List<RoleDTO> dtoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(entityList)) {
            Map<Long, List<RolePermissionDTO>> map = rolePermissionService.getByRoleIds(entityList.stream().map(Role::getId).collect(Collectors.toSet()));
            entityList.forEach(entity -> {
                RoleDTO dto = DataConverter.convert(entity, RoleDTO.class);
                dto.setPermissions(map.get(dto.getId()));
                dtoList.add(dto);
            });
        }
        return dtoList;
    }
}
